# -*- coding: utf-8 -*-
import threading
from .models import *
import datetime
import requests
from . import alg
import time
import json
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ProcessPoolExecutor

#+++++++++++++++++
# 双色球 1  
# 大乐透 2
# 3D    3
# 7星彩 4
# 排列3 5
# 排列5 6
# 七乐彩 7
#++++++++++++++++

def ssq():
    num = 0
    while True:
        # {'code': '2020023', 'red': [4, 5, 20, 21, 30, 33], 'blue': 8, 'date': '2020-04-12(日)'}
        last_code = alg.get_last_ssq_data()

        # 判断数据库是否存在此条数据
        status = False
        s = Open_history.objects.filter(lottery_id=1)
        if not s:
            status = True
        else:
            ss = s.last()
            # 格式化时间
            if int(ss.code) != int(last_code['code']):
                status = True

        if status is True:
            break
        num += 1
        if num >= 1:
            return
        time.sleep(600)

    s = Open_history()
    s.lottery_publisher = "福彩"
    s.lottery_name = "双色球"
    s.lottery_id = 1
    s.blue_list = json.dumps([last_code['blue']])
    s.red_list = json.dumps(last_code['red'])
    s.code = int(last_code['code'])
    s.open_date = last_code['date']
    s.save()

    # 执行预测下一期的算法
    alg.alg_ssq_001(last_code,s.pk)
    alg.alg_ssq_002(last_code,s.pk)


def dlt():
    num = 0
    while True:
        # {'code': '2020023', 'red': [4, 5, 20, 21, 30, 33], 'blue': [1,2], 'date': '2020-04-12'}
        last_code = alg.get_last_dlt_data()

        # 判断数据库是否存在此条数据
        status = False
        s = Open_history.objects.filter(lottery_id=2)
        if not s:
            status = True
        else:
            ss = s.last()
            if int(ss.code) != int(last_code['code']):
                status = True

        if status is True:
            break
        num += 1
        if num >= 1:
            return
        time.sleep(600)

    s = Open_history()
    s.lottery_publisher = "体彩"
    s.lottery_name = "大乐透"
    s.lottery_id = 2
    s.solida_list = json.dumps(last_code['blue'])
    s.orange_list = json.dumps(last_code['red'])
    s.code = int(last_code['code'])
    s.open_date = last_code['date']
    s.save()

    # 执行预测下一期的算法
    alg.alg_dlt_001(last_code,s.pk)

def fc3d():
    num = 0
    while True:
        # {'code': '2020023', 'red': [4, 5, 6], 'date': '2020-04-12(日)'}
        last_code = alg.get_last_3d_data()

        # 判断数据库是否存在此条数据
        status = False
        s = Open_history.objects.filter(lottery_id=3)
        if not s:
            status = True
        else:
            ss = s.last()
            if int(ss.code) != int(last_code['code']):
                status = True

        if status is True:
            break
        num += 1
        if num >= 1:
            return
        time.sleep(600)

    s = Open_history()
    s.lottery_publisher = "福彩"
    s.lottery_name = "3D"
    s.lottery_id = 3
    s.red_list = json.dumps(last_code['red'])
    s.code = int(last_code['code'])
    s.open_date = last_code['date']
    s.save()

    # 执行预测下一期的算法
    alg.alg_3d_001(last_code,s.pk)

def pl3():
    num = 0
    while True:
        # {'code': '2020023', 'red': [4, 5, 3,], 'date': '2020-04-12'}
        last_code = alg.get_last_pl3_data()

        # 判断数据库是否存在此条数据
        status = False
        s = Open_history.objects.filter(lottery_id=5)
        if not s:
            status = True
        else:
            ss = s.last()
            if int(ss.code) != int(last_code['code']):
                status = True

        if status is True:
            break
        num += 1
        if num >= 1:
            return
        time.sleep(600)

    s = Open_history()
    s.lottery_publisher = "体彩"
    s.lottery_name = "排列3"
    s.lottery_id = 5
    s.red_list = json.dumps(last_code['red'])
    s.code = int(last_code['code'])
    s.open_date = last_code['date']
    s.save()

    # 执行预测下一期的算法
    alg.alg_pl3_001(last_code,s.pk)

def pl5():
    num = 0
    while True:
        # {'code': '2020023', 'red': [4, 5, 3,6,9], 'date': '2020-04-12'}
        last_code = alg.get_last_pl5_data()

        # 判断数据库是否存在此条数据
        status = False
        s = Open_history.objects.filter(lottery_id=6)
        if not s:
            status = True
        else:
            ss = s.last()
            if int(ss.code) != int(last_code['code']):
                status = True

        if status is True:
            break
        num += 1
        if num >= 1:
            return
        time.sleep(600)

    s = Open_history()
    s.lottery_publisher = "体彩"
    s.lottery_name = "排列5"
    s.lottery_id = 6
    s.red_list = json.dumps(last_code['red'])
    s.code = int(last_code['code'])
    s.open_date = last_code['date']
    s.save()

    # 执行预测下一期的算法
    alg.alg_pl5_001(last_code,s.pk)

def qixincai():
    num = 0
    while True:
        # {'code': '2020023', 'red': [4, 5, 2,3,8,7,1], 'date': '2020-04-12'}
        last_code = alg.get_last_qixincai_data()

        # 判断数据库是否存在此条数据
        status = False
        s = Open_history.objects.filter(lottery_id=4)
        if not s:
            status = True
        else:
            ss = s.last()
            if int(ss.code) != int(last_code['code']):
                status = True

        if status is True:
            break
        num += 1
        if num >= 1:
            return
        time.sleep(600)

    s = Open_history()
    s.lottery_publisher = "体彩"
    s.lottery_name = "七星彩"
    s.lottery_id = 4
    s.red_list = json.dumps(last_code['red'])
    s.code = int(last_code['code'])
    s.open_date = last_code['date']
    s.save()

    # 执行预测下一期的算法
    alg.alg_qixincai_001(last_code,s.pk)

def qilecai():
    num = 0
    while True:
        # {'code': '2020023', 'red': [4, 5, 20, 21, 30, 23, 11], 'blue': 8, 'date': '2020-04-12(日)'}
        last_code = alg.get_last_qilecai_data()

        # 判断数据库是否存在此条数据
        status = False
        s = Open_history.objects.filter(lottery_id=7)
        if not s:
            status = True
        else:
            ss = s.last()
            if int(ss.code) != int(last_code['code']):
                status = True

        if status is True:
            break
        num += 1
        if num >= 1:
            return
        time.sleep(600)

    s = Open_history()
    s.lottery_publisher = "福彩"
    s.lottery_name = "七乐彩"
    s.lottery_id = 7
    s.blue_list = json.dumps([last_code['blue']])
    s.red_list = json.dumps(last_code['red'])
    s.code = int(last_code['code'])
    s.open_date = last_code['date']
    s.save()

    # 执行预测下一期的算法
    alg.alg_qlc_001(last_code,s.pk)

def main():
    # 新建计划任务
    executors = {
            'default': {'type': 'threadpool', 'max_workers': 100},
            'processpool': ProcessPoolExecutor(max_workers = 1)
            }
    job_defaults = {
        'coalesce': False,
        'max_instances': 3
    }
    instance = BackgroundScheduler()
    instance.configure(executors=executors, job_defaults=job_defaults)

    #测试
    # instance.add_job(ssq, 'interval', minutes=1,)

    # 双色球
    ssq()
    instance.add_job(ssq, 'interval', minutes=30,)
    # instance.add_job(ssq,'cron', day_of_week='tue,thu,sun',  hour=21, minute=20)

    # 大乐透
    dlt()
    instance.add_job(dlt, 'interval', minutes=30,)
    # instance.add_job(dlt,'cron', day_of_week='mon,wed,sat',  hour=21, minute=20)

    # 3d
    fc3d()
    instance.add_job(fc3d, 'interval', minutes=30,)
    # instance.add_job(fc3d,'cron', day_of_week='mon,tue,wed,thu,fri,sat,sun',  hour=21, minute=20)

    # pl3
    pl3()
    instance.add_job(pl3, 'interval', minutes=30,)
    # instance.add_job(pl3,'cron', day_of_week='mon,tue,wed,thu,fri,sat,sun',  hour=21, minute=20)

    # pl5
    pl5()
    instance.add_job(pl5, 'interval', minutes=30,)
    # instance.add_job(pl5,'cron', day_of_week='mon,tue,wed,thu,fri,sat,sun',  hour=21, minute=20)

    # qixincai
    qixincai()
    instance.add_job(qixincai, 'interval', minutes=30,)
    # instance.add_job(qixincai,'cron', day_of_week='tue,fri,sun',  hour=21, minute=20)

    # qilecai
    qilecai()
    instance.add_job(qilecai, 'interval', minutes=30,)
    # instance.add_job(qilecai,'cron', day_of_week='mon,wed,fri',  hour=21, minute=20)

    instance.start()

def run():
    try:
        t = threading.Thread(target=main)
        t.setDaemon(True)
        t.start()
    except BaseException as e:
        print(e)